﻿@using MvcContrib.UI.Grid
@model XRM.Security.WebUI.Models.SystemUser.ADUserDialogModel
@{
    ViewBag.Title = "Импорт пользователя из Active Directory";
    Layout = "~/Views/Shared/_LayoutDialog.cshtml";
}

<style type="text/css">
    #table_dialog_id tbody tr
    {
        cursor: pointer;
    }
    #table_dialog_id .selected td {
        background-color: #428bca;
        color: white;
    }
</style>

<div class="modal-body">
    @using (Html.BeginForm("ADUsersDialog", "SystemUser", FormMethod.Post, new { id = "dlgADUsersDialogForm" }))
    {      
        @Html.AntiForgeryToken()
        @Html.HiddenFor(m => m.SubmitAction)
        @Html.HiddenFor(m => m.SelectedFullName)
        @Html.HiddenFor(m => m.SelectedLogin)
        @Html.HiddenFor(m => m.DepartmentId)
        @Html.HiddenFor(m => m.DepartmentAddressId)
        
        <div>
            <div class="input-group col-sm-12">
                @Html.TextBoxFor(m => m.SearchString, new { @class = "form-control" })
                <span class="input-group-btn">
                    <button id="btnSearch" class="btn btn-default" type="submit" title="Поиск в AD"><i class="fa fa-search"></i></button>
                </span>
            </div>
            @Html.ValidationMessageFor(m => m.SearchString)
        </div>
    }    
        
     <div class="table-scrollable" style="height:200px;">    
    @{
        if(Model.ADUserList != null && Model.ADUserList.Any())
        {
            Html.Grid(Model.ADUserList).Columns(column =>
                {
                    column.For(item => item.FullName).Named("Имя пользователя");
                    column.For(item => item.AccountName).Named("Логин");
                }).Attributes(@class => "table table-striped table-bordered table-hover", id => "table_dialog_id").Render();
        }
        else if (Model.ADUserList != null)
        {
            <p>Пользователи не найдены.</p>
        }
    }
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-primary" id="btnChoise">Выбрать</button>
    <button type="button" class="btn btn-default" data-dismiss="modal">Отмена</button>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $('#btnSearch').on('click', function () { SearchUsers(); });
        $('#btnChoise').on('click', function () { ChoiseUser(); });
        $('#btnChoise').prop('disabled', true);
        $('#btnSearch').tooltip();
        
        @if (Model.ADUserList != null && Model.ADUserList.Any())
        {
            <text>

                $('#table_dialog_id').dataTable({
                    paging: false,
                    bFilter: false,
                    bSort: true,
                    info: false
                });
                $('#table_dialog_id tbody').on('click', 'tr', function () {
                    if ($(this).hasClass('selected')) {
                        $(this).removeClass('selected');
                        $('#btnChoise').prop('disabled', true);
                    }
                    else {
                        $('#table_dialog_id').dataTable().$('tr.selected').removeClass('selected');
                        $(this).addClass('selected');
                        $('#btnChoise').prop('disabled', false);
                    }
                });
            </text>
        }
    });

    function SearchUsers() {
        $('#SubmitAction').val("search");
        return true;
    }

    function ChoiseUser() {
        var trSelected = $('#table_dialog_id').dataTable().$('tr.selected');
        if (trSelected) {
            $('#SubmitAction').val("choise");
            $('#SelectedFullName').val(trSelected.find("td").eq(0).html());
            $('#SelectedLogin').val(trSelected.find("td").eq(1).html());
            $("#dlgADUsersDialogForm").submit();
        }
        else {
            alert('Нет выбранных пользователей.');
        }
    }
</script>
